<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户id" />
      </el-form-item>
      <el-form-item label="openid" prop="openid">
        <el-input v-model="formData.openid" placeholder="请输入openid" />
      </el-form-item>
      <el-form-item label="机构名称" prop="orgName">
        <el-input v-model="formData.orgName" placeholder="请输入机构名称" />
      </el-form-item>
      <el-form-item label="机构编码" prop="orgCode">
        <el-input v-model="formData.orgCode" placeholder="请输入机构编码" />
      </el-form-item>
      <el-form-item label="姓名" prop="patientName">
        <el-input v-model="formData.patientName" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNo">
        <el-input v-model="formData.idCardNo" placeholder="请输入身份证号" />
      </el-form-item>
      <el-form-item label="病人ID" prop="patientId">
        <el-input v-model="formData.patientId" placeholder="请输入病人ID" />
      </el-form-item>
      <el-form-item label="门诊号" prop="clinicNo">
        <el-input v-model="formData.clinicNo" placeholder="请输入门诊号" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="HIS收费单据号串(逗号拼接)" prop="hisBillNos">
        <el-input v-model="formData.hisBillNos" placeholder="请输入HIS收费单据号串(逗号拼接)" />
      </el-form-item>
      <el-form-item label="HIS订单号" prop="hisOrderNo">
        <el-input v-model="formData.hisOrderNo" placeholder="请输入HIS订单号" />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="formData.orderType" placeholder="请选择订单类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付订单号" prop="orderNumber">
        <el-input v-model="formData.orderNumber" placeholder="请输入支付订单号" />
      </el-form-item>
      <el-form-item label="付款码" prop="authCode">
        <el-input v-model="formData.authCode" placeholder="请输入付款码" />
      </el-form-item>
      <el-form-item label="渠道订单号" prop="thirdOrderNo">
        <el-input v-model="formData.thirdOrderNo" placeholder="请输入渠道订单号" />
      </el-form-item>
      <el-form-item label="订单金额" prop="amount">
        <el-input v-model="formData.amount" placeholder="请输入订单金额" />
      </el-form-item>
      <el-form-item label="其他费用" prop="otherAmount">
        <el-input v-model="formData.otherAmount" placeholder="请输入其他费用" />
      </el-form-item>
      <el-form-item label="0 未支付 1 支付成功 2 已退款 3 申请退款" prop="payStatus">
        <el-radio-group v-model="formData.payStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          v-model="formData.payTime"
          type="date"
          value-format="x"
          placeholder="选择支付时间"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="formData.payType" placeholder="请选择支付方式">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="退款订单号" prop="refundNumber">
        <el-input v-model="formData.refundNumber" placeholder="请输入退款订单号" />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundPrice">
        <el-input v-model="formData.refundPrice" placeholder="请输入退款金额" />
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker
          v-model="formData.refundTime"
          type="date"
          value-format="x"
          placeholder="选择退款时间"
        />
      </el-form-item>
      <el-form-item label="退款原因" prop="refundReason">
        <el-input v-model="formData.refundReason" placeholder="请输入退款原因" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { QrcodePayApi, QrcodePayVO } from '@/api/hospital/qrcodepay'

/** 扫码支付 表单 */
defineOptions({ name: 'QrcodePayForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  openid: undefined,
  orgName: undefined,
  orgCode: undefined,
  patientName: undefined,
  idCardNo: undefined,
  patientId: undefined,
  clinicNo: undefined,
  phone: undefined,
  hisBillNos: undefined,
  hisOrderNo: undefined,
  orderType: undefined,
  orderNumber: undefined,
  authCode: undefined,
  thirdOrderNo: undefined,
  amount: undefined,
  otherAmount: undefined,
  payStatus: undefined,
  payTime: undefined,
  payType: undefined,
  refundNumber: undefined,
  refundPrice: undefined,
  refundTime: undefined,
  refundReason: undefined,
  remark: undefined,
  status: undefined
})
const formRules = reactive({
  orderType: [{ required: true, message: '订单类型不能为空', trigger: 'change' }],
  orderNumber: [{ required: true, message: '支付订单号不能为空', trigger: 'blur' }],
  amount: [{ required: true, message: '订单金额不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await QrcodePayApi.getQrcodePay(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as QrcodePayVO
    if (formType.value === 'create') {
      await QrcodePayApi.createQrcodePay(data)
      message.success(t('common.createSuccess'))
    } else {
      await QrcodePayApi.updateQrcodePay(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    openid: undefined,
    orgName: undefined,
    orgCode: undefined,
    patientName: undefined,
    idCardNo: undefined,
    patientId: undefined,
    clinicNo: undefined,
    phone: undefined,
    hisBillNos: undefined,
    hisOrderNo: undefined,
    orderType: undefined,
    orderNumber: undefined,
    authCode: undefined,
    thirdOrderNo: undefined,
    amount: undefined,
    otherAmount: undefined,
    payStatus: undefined,
    payTime: undefined,
    payType: undefined,
    refundNumber: undefined,
    refundPrice: undefined,
    refundTime: undefined,
    refundReason: undefined,
    remark: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>